package com.zy;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author zhaoyi
 * @date 2019/10/23
 * 集合线程安全
 */
public class ListSafe {

    public static void main(String[] args) {

        /**
         * 初始值 DEFAULT_CAPACITY 10
         * 懒加载
         */
        //List<String> list = new ArrayList();

        /**
         * java.util.ConcurrentModificationException
         * 并发修改异常
         */
        //解决方法一
        //Collections.synchronizedCollection(list);

        //解决方法二
        List<String> list = new CopyOnWriteArrayList();


        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 5));
                System.out.println(list);
            }).start();
        }

        /**
         * hashSet 底层是HashMap
         * public boolean add(E e)
         *      return map.put(e, PRESENT)==null
         */
        HashSet hashSet = new HashSet();


    }

}
